//
//  XJAddressViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/21.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJAddressViewController.h"
#import "XJNoAddressView.h"
//Controller
#import "XJAddNewAddressViewController.h"
#import "XJModifyAddressViewController.h" //6-29 修改收货地址
//view
#import "XJAddressCell.h"

// 控制器瘦身
#import "XJAddressTableViewProxy.h" // tableView代理
#import "XJAddressFormat.h" // 网络请求、业务逻辑处理

@interface XJAddressViewController ()<XJAddressFormatDelegate>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) XJNoAddressView *noAddressView;

@property(nonatomic, strong) XJAddressTableViewProxy *addressTableVeiwProxy;
@property(nonatomic, strong) XJAddressFormat *addressFormat;

@end
//Class 'XJAddressViewController' does not conform to protocol 'UITableViewDataSource'
@implementation XJAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupView];
    
    [self requestAddressListData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)setupView {
    self.navigationItem.title = @"收货地址";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"新增" style:(UIBarButtonItemStylePlain) target:self action:@selector(addAddressAction)];
    
    [self.view addSubview:self.tableView];
    self.tableView.frame = CGRectMake(0,0, KWidth, KHeight - XJTopNavH); // 要减去导航条高度。
}

- (void)requestAddressListData {
    [self.addressFormat requestAddresList];
}

- (void)addAddressAction{
    XJLog(@"新增收货地址");
    
    XJAddNewAddressViewController *addNewVC = [[XJAddNewAddressViewController alloc] init];
    
    [self.navigationController pushViewController:addNewVC animated:YES];
}

#pragma mark - lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        
        _tableView.delegate = self.addressTableVeiwProxy;
        _tableView.dataSource = self.addressTableVeiwProxy;
        
        [_tableView registerClass:[XJAddressCell class] forCellReuseIdentifier:@"XJAddressCellId"];
        _tableView.tableHeaderView = [UIView new];
        _tableView.tableFooterView = [UIView new];
        
        _tableView.rowHeight = 130;
    }
    return _tableView;
}

- (XJNoAddressView *)noAddressView {
    if (!_noAddressView) {
        _noAddressView = [[XJNoAddressView alloc] initWithFrame:CGRectMake(0, 100, KWidth, KHeight-100)];
        
        XJWeakSelf(self)
        _noAddressView.noAddressViewAddAddressBlock = ^{
            [weakself addAddressAction];
        };
    }
    return _noAddressView;
}

- (XJAddressTableViewProxy *)addressTableVeiwProxy {
    if (!_addressTableVeiwProxy) {
        _addressTableVeiwProxy = [[XJAddressTableViewProxy alloc] init];
        
        XJWeakSelf(self);
        // 到这里逻辑有点乱了。
        // 设置默认地址
        _addressTableVeiwProxy.addressTableProxySelectAddressBlock = ^(BOOL isSelect, NSIndexPath *indePath) {
            [weakself.addressFormat selectAddressAtIndexPath:indePath isSelected:isSelect];
        };
        
        // 删除地址
        _addressTableVeiwProxy.addressTableProxyDeleteBlock = ^(NSIndexPath *indexPath) {
            [weakself.addressFormat deleteAddressAtIndexPath:indexPath];
        };
        
        // 编辑地址
        _addressTableVeiwProxy.addressTableProxyEditBlock = ^(XJAddressModel *addressM) {
            // 跳转编辑地址控制器
            XJModifyAddressViewController *modifyVC = [[XJModifyAddressViewController alloc] init];

            modifyVC.addressM = addressM;
            
            [weakself.navigationController pushViewController:modifyVC animated:YES];
        };
       
        // didSelectRow
        _addressTableVeiwProxy.addressTableProxyDidSelectedRowBlock = ^(XJAddressModel *addressModel) {
            //将model传递出去 + 返回到上一个控制器。
            XJLog(@"%@",addressModel);
            
            //这一行代码会奔溃。？？？？？看从什么地方进来。 从 个人中心--->个人设置---> 收货地址会奔溃。估计是循环引用了。 暂时没时间改。
            weakself.passAddresModelBlock(addressModel);
            [weakself.navigationController popViewControllerAnimated:YES];
        };
        
    }
    return _addressTableVeiwProxy;
}

- (XJAddressFormat *)addressFormat {
    if (!_addressFormat) {
        _addressFormat = [[XJAddressFormat alloc] init];
        _addressFormat.delegate = self;
    }
    return _addressFormat;
}

#pragma mark -  XJAddressFormatDelegate
- (void)addressFormatRequestAddressListDidSuccessWithArray:(NSMutableArray *)dataArray {
    /**两件事:
     1.传递数据给 XJAddressTableViewProxy
     2.刷新表格。
    */
    
    // 有就显示 如果没有数据就显示占位图片
    if (dataArray.count) {
        self.addressTableVeiwProxy.dataArray = dataArray;
        [self.tableView reloadData];
        
        [self.noAddressView xj_hideNoAddressView];
    }else {
        [self.noAddressView xj_showNoAddressViewInView:self.view];
    }
}

- (void)addressFormatRequestAddressListFailed:(NSError *)err {
    [JRToast showWithText:@"获取地址列表数据失败"];
    XJLog(@"%@",err);
    [self.noAddressView xj_showNoAddressViewInView:self.view];
}

// 需要数据时的回调。
- (void)selectAddressAndReloadData {
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

